// AnjunaOpenGL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
                                        int x, int y) {    //The current mouse coordinates
        switch (key) {
                case 27: //Escape key
                        exit(0); //Exit the program
        }
}

//Initializes 3D rendering
void initRendering() {
        //Makes 3D drawing work when something is in front of something else
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);
        glEnable(GL_COLOR_MATERIAL);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

//Called when the window is resized
void handleResize(int w, int h) {
        //Tell OpenGL how to convert from coordinates to pixel values
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

        //Set the camera perspective
        glLoadIdentity(); //Reset the camera
        gluPerspective(45.0,                  //The camera angle
                                   (double)w / (double)h, //The width-to-height ratio
                                   1.0,                   //The near z clipping coordinate
                                   200.0);                //The far z clipping coordinate
}
float _angle = 0.0f;
float _cameraAngle = 0.0f;

void drawColumn() {
        glBegin(GL_QUADS); 
        glVertex3f(0.7f, -0.5f, 0.0f);
        glVertex3f(0.85f, -0.5f, 0.0f);
        glVertex3f(0.15f, 0.5f, 0.0f);
        glVertex3f(0.0f, 0.5f, 0.0f);		
        glEnd();
}
void drawRect() {
        glBegin(GL_QUADS);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.65f, 0.0f, 0.0f);
        glVertex3f(0.55f, 0.15f, 0.0f);
        glVertex3f(-0.1f, 0.15f, 0.0f);
        glEnd();
}
void drawAnjunasign() {
        glColor3f(0.0f, 1.0f, 0.0f);
		
        glBegin(GL_QUADS); //Arrow holder
        glVertex3f(-0.3f, 0.0f, 0.0f);
        glVertex3f(-0.5f, 0.0f, 0.0f);
        glVertex3f(-0.1f, 0.65f, 0.0f);	
        glVertex3f(0.0f, 0.5f, 0.0f);	
        glEnd();

		glPushMatrix();//Arrow
        glTranslatef(-0.5f, 0.0f, 0.0f);
		glBegin(GL_TRIANGLES); 
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(0.5f, 0.0f, 0.0f);
        glVertex3f(0.25f, -0.25f, 0.0f);	
        glEnd();
		glPopMatrix();

		drawColumn();//Left collumn

		glPushMatrix();//Right collumn
        glTranslatef(0.5f, 0.0f, 0.0f);
		drawColumn();
		glPopMatrix();

		glPushMatrix(); //Top
        glTranslatef(0.0f, 0.5f, 0.0f);
		drawRect();
		glPopMatrix();
		
		glPushMatrix(); //Down
        glTranslatef(1.35f, -0.5f, 0.0f);
		glRotatef(180,0,0,1);
		drawRect();
		glPopMatrix();
}
void drawWalls(float width) {
        glPushMatrix ();//Save transformations
        glRotatef (90,0,1,0);
        glTranslatef (0.0f, 0.0f, -0.5f);

        glBegin(GL_QUADS); //Arrow holder
        glVertex3f(0.0, 0.0f, 0.0f);
        glVertex3f(0.0, 0.65f, 0.4f);
		glVertex3f(-width, 0.65f, 0.4f);	
        glVertex3f(-width, 0.0f, 0.0f);	
        glEnd(); 
		
        glTranslatef (0.0f, 0.0f, 0.2f);
        glBegin(GL_QUADS); //Arrow holder
        glVertex3f(0.0, 0.0f, 0.0f);
        glVertex3f(0.0, 0.65f, 0.4f);
		glVertex3f(-width, 0.65f, 0.4f);	
        glVertex3f(-width, 0.0f, 0.0f);	
        glEnd();
		
        glTranslatef (0.0f, 0.0f, 0.85f);
        glBegin(GL_QUADS); //Right collumn
        glVertex3f(0.0f, -0.65f, 0.9f);
        glVertex3f(0.0f, 0.65f, 0.0f);
        glVertex3f(-width, 0.65f, 0.0f);
        glVertex3f(-width, -0.65f, 0.9f);		
        glEnd();

        glTranslatef (0.0f, 0.0f, -0.15f);
        glBegin(GL_QUADS); //Right collumn
        glVertex3f(0.0f, -0.65f, 0.9f);
        glVertex3f(0.0f, 0.65f, 0.0f);
        glVertex3f(-width, 0.65f, 0.0f);
        glVertex3f(-width, -0.65f, 0.9f);		
        glEnd();

        glTranslatef (0.0f, 0.0f, -0.35f);
        glBegin(GL_QUADS); //Left collumn
        glVertex3f(0.0f, -0.65f, 0.9f);
        glVertex3f(0.0f, 0.65f, 0.0f);
        glVertex3f(-width, 0.65f, 0.0f);
        glVertex3f(-width, -0.65f, 0.9f);		
        glEnd();
		
        glTranslatef (0.0f, 0.0f, -0.15f);
        glBegin(GL_QUADS); //Left collumn
        glVertex3f(0.0f, -0.65f, 0.9f);
        glVertex3f(0.0f, 0.65f, 0.0f);
        glVertex3f(-width, 0.65f, 0.0f);
        glVertex3f(-width, -0.65f, 0.9f);		
        glEnd();

		glPopMatrix();
}
//Draws the 3D scene
void drawScene() {
        //Clear information from last draw
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
        glLoadIdentity(); //Reset the drawing perspective
        glRotatef (_cameraAngle,0,1,0);
        glTranslatef (0.0f, 0.0f, -5.0f);//Move forward 5units
		
        glRotatef (_angle,0,1,0);
        glPushMatrix ();//Save transformations
		drawAnjunasign();
		drawWalls(0.2);
        glTranslatef(0.0f, 0.0f, 0.2f);
		drawAnjunasign();
		glPopMatrix();
        glutSwapBuffers(); //Send the 3D scene to the screen
}
void update(int value) {
    _angle += 1.0f;
    if (_angle > 360) {
        _angle -= 360;
    }

    glutPostRedisplay(); //Tell GLUT that the scene has changed

    //Tell GLUT to call update again in 25 milliseconds
    glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(400, 400); //Set the window size

    //Create the window
    glutCreateWindow("AnjunaOpenGL");
    initRendering(); //Initialize rendering

    //Set handler functions for drawing, keypresses, and window resizes
    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0); //Add a timer
    glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
    return 0; //This line is never reached
}
